
// WebGUIView.cpp : implementation of the CWebGUIView class
//

#include "stdafx.h"
#include "WebGUI.h"

#include "WebGUIDoc.h"
#include "WebGUIView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CWebGUIView

IMPLEMENT_DYNCREATE(CWebGUIView, CFormView)

BEGIN_MESSAGE_MAP(CWebGUIView, CFormView)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_STARTRECORD, &CWebGUIView::OnBnClickedStartrecord)
	ON_BN_CLICKED(IDC_STOPRECORD, &CWebGUIView::OnBnClickedStopreord)
END_MESSAGE_MAP()

// CWebGUIView construction/destruction

CWebGUIView::CWebGUIView()
	: CFormView(CWebGUIView::IDD)
{
	// TODO: add construction code here
	m_pFrame = NULL ;
}

CWebGUIView::~CWebGUIView()
{
}

void CWebGUIView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
}

BOOL CWebGUIView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

UINT ListenThread (PVOID p);

#include "MainFrm.h"
void CWebGUIView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();

	SetTimer(1,1000,NULL) ;
	m_pFrame = (CMainFrame *)GetParentFrame() ;

	GetDlgItem(IDC_STARTRECORD)->EnableWindow(TRUE);
	GetDlgItem(IDC_STOPRECORD)->EnableWindow(FALSE);


}

void CWebGUIView::OnRButtonUp(UINT nFlags, CPoint point)
{
	ClientToScreen(&point);
	OnContextMenu(this, point);
}

void CWebGUIView::OnContextMenu(CWnd* pWnd, CPoint point)
{
	theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
}


// CWebGUIView diagnostics

#ifdef _DEBUG
void CWebGUIView::AssertValid() const
{
	CFormView::AssertValid();
}

void CWebGUIView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CWebGUIDoc* CWebGUIView::GetDocument() const // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CWebGUIDoc)));
	return (CWebGUIDoc*)m_pDocument;
}
#endif //_DEBUG


// CWebGUIView message handlers

void CWebGUIView::OnUpdate(CView* /*pSender*/, LPARAM /*lHint*/, CObject* /*pHint*/)
{
	CWebGUIDoc *pDoc = GetDocument() ;

	UpdatePropertyView();
	if (!pDoc->m_bConnected)
	{
		SetDlgItemText(IDC_EDIT1, "Disconnected") ;
		SetDlgItemText(IDC_EDIT2, "---") ;
		SetDlgItemText(IDC_EDIT3, "---") ;
		
	}
	else
	{
		SetDlgItemText(IDC_EDIT1, "Connected") ;
		
		CString strTime ;
		strTime.Format("%4d/%02d/%02d - %02d:%02d:%02d", pDoc->m_stLastUpdate.wYear,
			pDoc->m_stLastUpdate.wMonth,
			pDoc->m_stLastUpdate.wDay,
			pDoc->m_stLastUpdate.wHour,
			pDoc->m_stLastUpdate.wMinute,
			pDoc->m_stLastUpdate.wSecond) ;

		SetDlgItemText(IDC_EDIT2, strTime) ;

		CTime t2(pDoc->m_stLastUpdate) ;
		CTime t1(pDoc->m_stStartTime) ;

		CTimeSpan t= t2-t1 ;

		if (t.GetTotalSeconds()<60)
		{
			strTime.Format("%d seconds", t.GetTotalSeconds()) ;
		}
		else
		{
			if (t<3600)
			{
				strTime.Format("%d minutes", t.GetTotalSeconds()) ;
			}
			else
			{
				strTime.Format("%d Hours and %d minutes", t.GetTotalSeconds()/3600 , ( t.GetTotalSeconds() - 3600*(t.GetTotalSeconds()/3600 ))/60 ) ;
			}
		}
		SetDlgItemText(IDC_EDIT3, strTime) ;
	}
	// TODO: Add your specialized code here and/or call the base class
}

void CWebGUIView::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: Add your message handler code here and/or call default

	// this time is used in order to check if we are connected to FM 
	if (GetDocument()->m_bConnected)
	{
		SYSTEMTIME st;
		GetLocalTime(&st) ;
		CTimeSpan ts = CTime(st) - CTime(GetDocument()->m_stLastUpdate) ;
		if (ts.GetTotalSeconds()>2000)
		{
			GetDocument()->m_bConnected = FALSE ;
			GetDocument()->OnChangeConnection();
		}
	}
	if (GetDocument()->m_bShouldUpdate)
	{
		GetDocument()->UpdateAllViews(NULL);
		GetDocument()->m_bShouldUpdate = FALSE ;
	}

	CFormView::OnTimer(nIDEvent);
}

// this method is used to update information about the system like the latest update time and weather we are connected to FM or not
void CWebGUIView::UpdatePropertyView()
{
	if (! (m_pFrame && IsWindow(m_pFrame->GetSafeHwnd())))
	{
		return ;
	}
	if (GetDocument()->m_bConnected)
	{
		CString strWorking = "Not Working";
		CString strCPU = "---";
		long nTotalRequestCount = 0 ;
		long nWebServerCount = 0 ;
		for (int n=0;n<GetDocument()->m_aNodes.GetCount();n++)
		{
			if (atol(GetDocument()->m_aNodes[n].strType)==1)
			{
				strWorking = "Working" ;
				strCPU.Format("%.1f %%", (float)atol(GetDocument()->m_aNodes[n].strCPU)/10) ;

			}
			else
				if (atol(GetDocument()->m_aNodes[n].strType)==1)
				{
					nTotalRequestCount+=atol(GetDocument()->m_aNodes[n].strRequestCount) ;
					nWebServerCount++;
				}
		}
		m_pFrame->m_wndProperties.m_wndPropList.GetProperty(0)->GetSubItem(0)->SetValue(strWorking) ;
		m_pFrame->m_wndProperties.m_wndPropList.GetProperty(0)->GetSubItem(1)->SetValue(strCPU) ;

		m_pFrame->m_wndProperties.m_wndPropList.GetProperty(1)->GetSubItem(0)->SetValue(nWebServerCount) ;
		m_pFrame->m_wndProperties.m_wndPropList.GetProperty(1)->GetSubItem(1)->SetValue(nTotalRequestCount) ;



	}
	else
	{
		m_pFrame->m_wndProperties.m_wndPropList.GetProperty(0)->GetSubItem(0)->SetValue("---") ;
		m_pFrame->m_wndProperties.m_wndPropList.GetProperty(0)->GetSubItem(1)->SetValue("---") ;

		m_pFrame->m_wndProperties.m_wndPropList.GetProperty(1)->GetSubItem(0)->SetValue(0L) ;
		m_pFrame->m_wndProperties.m_wndPropList.GetProperty(1)->GetSubItem(1)->SetValue(0L) ;

	}
}


// starts record the node's data into log files
void CWebGUIView::OnBnClickedStartrecord()
{
	GetDocument()->m_bRecording = TRUE ;
	GetDlgItem(IDC_STARTRECORD)->EnableWindow(FALSE);
	GetDlgItem(IDC_STOPRECORD)->EnableWindow(TRUE);
}

// stops recording
void CWebGUIView::OnBnClickedStopreord()
{
	GetDocument()->m_bRecording = FALSE ;
	GetDlgItem(IDC_STARTRECORD)->EnableWindow(TRUE);
	GetDlgItem(IDC_STOPRECORD)->EnableWindow(FALSE);
}
